﻿<html>
<head>
    <title>Run</title>
    <script type="text/javascript" src="../Scripts/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="../Scripts/Linq2IndexedDB.js"></script>
    <script type="application/javascript;version=1.7">


            window.Continuation = {}
            window.Suspend = {}


            function run(gen){
                function isGenerator(obj){
                    return String(obj) === '[object Generator]'
                }
                function last(arr){
                    return arr[arr.length - 1]
                }
                function startBouncing(retval){
                    try{
                        while (true){
                            if (isGenerator(retval)){
                                stack.push(retval)
                                retval = undefined
                            }else if (retval === Suspend){
                                break                       // to suspend execution:
                                // just break out of this
                                // while-loop.
                                // we can later resume
                                // by re-starting another
                                // while-loop by calling
                                // startBouncing() again
                            }else if (retval === Continuation){
                                retval = startBouncing      // send startBouncing
                                // back as the callback
                                // handler to the async
                                // function call
                            }else{
                                stack.pop().close()
                            }
                            gen = last(stack)
                            if (!gen) break
                            retval = gen.send(retval)
                        }
                    }catch(e){
                        //if e === StopIteration;
                        console.log('Program ended.');
                    }
                }    
                var stack = []
                var retval
                startBouncing(gen)
            }


            function _xhr(url, callback){
                var xhr = new XMLHttpRequest()
                xhr.open('GET', url)
                xhr.onreadystatechange = function(){
                    if (this.readyState == 4){
                        // send the responseText as the first arg of the continuation
                        callback(this.responseText)
                    }
                }
                xhr.send()
            }


            function sleep(millis) {
                setTimeout((yield Continuation), millis);
                yield Suspend
            }


            function xhr(url){
                _xhr(url, (yield Continuation));
                yield Suspend
            }
            
        
            function get(args){
                var db = linq2indexedDB("Consumption database");
                db.linq.from("ConsumptionType").get(args).then(yield Continuation)
                yield Suspend;
            }

            function main(){
                yield sleep(1000)
                var greeting = yield xhr('/something.ajax?greeting')
                var who = yield xhr('/else.ajax?who&greeting=' + greeting)
                var type = yield get(1);
                alert(type);
                console.log(greeting + ' ' + who)
            }


            run(main())
</script>
</head>
<body>
</body>
</html>
